﻿/**********************************************************
*
* 模块名称：Web层公共方法类
* 当前版本：1.0
* 添加人员：楚涛
* 开始时间：2010-10-10
* 版本历史：v1.0
*
***********************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Database.Extend;

namespace LXChuTao.ECG.Demo.WebUI
{
    public static class WebFunHelper
    {
        #region 添加GridView 显示空行表头辅助方法
        /// <summary>
        /// 是否添加DataTable空行
        /// </summary>
        /// <param key="dt"></param>
        public static void IsAddEmptyRow(DataTable dt)
        {
            if (dt != null && dt.Rows.Count == 0)
            {
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
            }
        }

        /// <summary>
        /// 是否添加DataTable空行
        /// </summary>
        /// <param key="dt"></param>
        /// <param key="columns">隐藏某些列</param>
        public static void IsAddEmptyRow(DataTable dt, GridView gv, int[] columns)
        {
            if (dt != null && dt.Rows.Count == 0)
            {
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);

                foreach (int col in columns)
                {
                    if (gv.Columns.Count > col) gv.Columns[col].Visible = false;
                }
            }
        }

        /// <summary>
        /// 是否添加DataView空行
        /// </summary>
        /// <param key="dt"></param>
        /// <param key="columns">隐藏某些列</param>
        public static void IsAddEmptyRow(DataView dt, GridView gv, int[] columns)
        {
            if (dt != null && dt.Count == 0)
            {
                dt.AddNew();
                foreach (int col in columns)
                {
                    if (gv.Columns.Count > col) gv.Columns[col].Visible = false;
                }
            }
        }
        #endregion

        #region 赋值下拉列表框
        /// <summary>
        /// 根据下拉框的Value值赋值于下拉列表框
        /// </summary>
        /// <param name="drpSource">带有绑定数据源的下拉框</param>
        /// <param name="strAimValue">需要赋的Value值</param>
        public static void SelectDropDownListByValue(System.Web.UI.WebControls.DropDownList drpSource, string strAimValue)
        {
            int intCount = drpSource.Items.Count;
            for (int i = 0; i < intCount; i++)
            {
                if (drpSource.Items[i].Value == strAimValue)
                {
                    drpSource.SelectedIndex = i;
                    return;
                }
            }
        }
        /// <summary>
        /// 根据下拉框的Text值赋值于下拉列表框
        /// </summary>
        /// <param name="drpSource">带有绑定数据源的下拉框</param>
        /// <param name="strAimText">需要赋的Text值</param>
        public static void SelectDropDownListByText(System.Web.UI.WebControls.DropDownList drpSource, string strAimText)
        {
            int intCount = drpSource.Items.Count;
            strAimText = (strAimText + "").Trim();
            for (int i = 0; i < intCount; i++)
            {
                if (drpSource.Items[i].Text.Trim() == strAimText)
                {
                    drpSource.SelectedIndex = i;
                    return;
                }
            }
        }
        #endregion

        #region public static string GetKeyId 主键唯一ID
        /// <summary>
        /// 主键唯一ID字符串
        /// </summary>
        public static string GetKeyId
        {
            get { return CommonHelp.GetID25(); }
        }
        #endregion

        #region public static string GetHTMLCodeText 获取超文本Text值
        /// <summary>
        /// 获取超文本Text值
        /// </summary>
        /// <param name="strHtml">超文本字符串</param>
        /// <returns>返回超文本字符串的Text值</returns>
        public static string GetHTMLCodeText(string strHtml)
        {
            int indexStart = strHtml.IndexOf("<", 0);
            int indexEnd = strHtml.IndexOf(">", 0);
            while (indexStart != -1 && indexEnd > indexStart)
            {
                strHtml = strHtml.Remove(indexStart, indexEnd - indexStart + 1);
                indexStart = strHtml.IndexOf("<", 0);
                if (indexStart != -1) indexEnd = strHtml.IndexOf(">", indexStart);
            }
            return strHtml;
        }
        #endregion

        #region public static void ShowMessage 弹出消息
        /// <summary>
        /// 弹出消息
        /// </summary>
        /// <param name="Page">消息发生页面</param>
        /// <param name="Message">消息内容</param>
        public static void ShowMessage(System.Web.UI.Page Page, string Message)
        {
            if (Message == null)
            {
                Message = string.Empty;
            }
            string script = "<script language='jscript'>\n"
                          + "  alert(unescape(\"" + Microsoft.JScript.GlobalObject.escape(Message) + "\"));\n"
                          + "</script>";
            Page.RegisterStartupScript("MSG____________ID", script);
        }
        /// <summary>
        /// 弹出消息
        /// </summary>
        /// <param name="Page">消息发生页面</param>
        /// <param name="Message">消息内容</param>
        public static void ShowMessage1(System.Web.UI.Page Page, string Message)
        {
            if (Message == null)
            {
                Message = string.Empty;
            }
            string script = "<script language='javascript'>\n"
                          + "  alert(unescape(\"" + Microsoft.JScript.GlobalObject.escape(Message) + "\"));\n"
                          + "</script>";
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "MSG__ID", script);
        }
        #endregion
    }
}
